package com.fzk.blog.settings.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fzk.blog.bean.Type;
import com.fzk.blog.bean.vo.TypeInfo;
import com.fzk.blog.exception.MyException;
import com.fzk.blog.settings.service.ITypeService;
import com.fzk.blog.utils.MyJsonUtil;
import com.fzk.blog.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author fzk
 * @date 2021-08-16 17:23
 */
@RestController
public class TypeController {
    @Resource
    private ITypeService typeService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @PostMapping(path = "/type")
    @SaCheckRole("root")//检查角色
    public Map<String, Object> insertType(
            @RequestParam("name") String name,
            @RequestParam("description") String description
    ) throws JsonProcessingException {
        // 1.检查参数
        checkArgs(name, description);

        // 2.装入参数
        Type type = new Type();
        type.setName(name);
        type.setDescription(description);

        // 3.取出loginId
        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());

        // 4.调用service层
        TypeInfo typeInfo = typeService.insertType(type, loginId);

        //5. 返回结果
        return ResponseResult.success("增加成功", typeInfo);
    }

    @PutMapping(path = "/type")
    @SaCheckRole("root")//检查角色
    public Map<String, Object> updateType(
            @RequestParam("type_id") int typeId,
            @RequestParam("name") String name,
            @RequestParam("description") String description
    ) throws JsonProcessingException {
        // 1.参数检查
        checkArgs(name, description);

        // 2.装入参数
        Type type = new Type();
        type.setId(typeId);
        type.setName(name);
        type.setDescription(description);

        // 3.取出loginId
        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());

        // 4.调用service
        TypeInfo typeInfo = typeService.updateType(type, loginId);

        // 5.返回结果
        return ResponseResult.success("修改成功", typeInfo);
    }

    @DeleteMapping(path = "/type")
    @SaCheckRole(value = {"root"})//检查角色
    public Map<String, Object> deleteType(
            @RequestParam("type_id") int typeId
    ) {
        // 1,取出loginId
        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());
        TypeInfo typeInfo = typeService.deleteType(typeId, loginId);
        return ResponseResult.success("删除成功", typeInfo);
    }

    @GetMapping(path = "/typeInfo")
    public Map<String, Object> getTypeInfoOrList(
            @RequestParam(name = "type_id", required = false) String typeId
    ) throws JsonProcessingException {
        // 1.查询分类列表
        if (typeId == null) {
            List<TypeInfo> typeInfoList = typeService.getTypeInfoList();
            return ResponseResult.success("查询成功", typeInfoList);
        }
        // 2.查询特定分类
        else {
            TypeInfo typeInfo = typeService.getTypeById(Integer.parseInt(typeId));
            return ResponseResult.success("查询成功", typeInfo);
        }
    }

    /**
     * 参数检查
     *
     * @param args 可变形参
     */
    private void checkArgs(String... args) {
        for (String arg : args) {
            if (arg == null || arg.length() == 0)
                throw new MyException(400, "参数错误：" + arg);
        }
    }
}
